# coding:utf-8
import pandas as pd
from fastapi import APIRouter
from FastApi_test.conf import conn_std
from FastApi_test.idcard_check import  User

router = APIRouter(prefix="/marriage", tags=["婚姻信息"])

"""
结婚登记台账       std.std_smzj_hy_jhdjtz
补领结婚证台账      std.std_smzj_hy_bljhztz
离婚登记台账       std.std_smzj_hy_lhdjtz
补领离婚证台账      std.std_smzj_hy_bllhztz
"""




@router.post(path="/legal_person_marriage_status", summary="企业法人最新婚姻状态")
# def calc_legal_person_marriage_status(data=Body(None)):
def calc_legal_person_marriage_status(data:User):
    idcard = data.dict()["idcard"]
    query1 = f"""
		select 
			jhdjrq
		from std.std_smzj_hy_jhdjtz 
		where sfzjh_n = '{idcard}' or sfzjh_nv = '{idcard}' 
		order by jhdjrq desc limit 1"""
    query2 = f"""
		select 
			lhdjrq
		from std.std_smzj_hy_lhdjtz 
		where sfzjh_n = '{idcard}' or sfzjh_nv = '{idcard}' 
		order by lhdjrq desc limit 1"""

    df1 = pd.read_sql(query1, conn_std)
    df2 = pd.read_sql(query2, conn_std)

    if len(df1) == 0:
        return {
            "msg": "企业法人最新婚姻状态",
            "idcard": idcard,
            "status": "未婚"
        }
    elif (len(df1) > 0) and (len(df2) == 0):
        df1_jhdjrq = df1.iloc[0]["jhdjrq"]
        return {
            "msg": "企业法人最新婚姻状态",
            "idcard": idcard,
            "status": "已婚",
            "jhdjrq": df1_jhdjrq

        }
    elif (len(df1) > 0) and (len(df2) > 0):
        df1_jhdjrq = df1.iloc[0]["jhdjrq"]
        df2_lhdjrq = df2.iloc[0]["lhdjrq"]
        if df1_jhdjrq > df2_lhdjrq:
            return {
                "msg": "企业法人最新婚姻状态",
                "idcard": idcard,
                "status": "已婚",
                "jhdjrq": df1_jhdjrq
            }
        else:
            return {
                "msg": "企业法人最新婚姻状态",
                "idcard": idcard,
                "status": "离异",
                "lhdjrq": df2_lhdjrq
            }
#
#
# @router.post(path="/legal_person_marriage_count", summary="企业法人婚姻次数")
# def calc_legal_person_marriage_count(data=Body(None)):
#     idcard = data["idcard"]
#     query1 = f"""
# 			select
# 				count(*) as cnt
# 			from std.std_smzj_hy_jhdjtz
# 			where sfzjh_n = '{idcard}' or sfzjh_nv = '{idcard}' """
#     cnt = int(pd.read_sql(query1, conn_std).iloc[0]["cnt"])
#
#     return {
#         "msg": "企业法人婚姻次数",
#         "idcard": idcard,
#         "count": cnt
#     }
#
#
# @router.post(path="/legal_person_latest_marriage_date", summary="企业法人最新婚姻状态时间")
# def calc_legal_person_latest_marriage_date(data=Body(None)):
#     # 见企业法人最新婚姻状态接口
#     idcard = data["idcard"]
#     query1 = f"""
# 		select
# 			jhdjrq
# 		from std.std_smzj_hy_jhdjtz
# 		where sfzjh_n = '{idcard}' or sfzjh_nv = '{idcard}'
# 		order by jhdjrq desc limit 1"""
#     query2 = f"""
# 		select
# 			lhdjrq
# 		from std.std_smzj_hy_lhdjtz
# 		where sfzjh_n = '{idcard}' or sfzjh_nv = '{idcard}'
# 		order by lhdjrq desc limit 1"""
#
#     df1 = pd.read_sql(query1, conn_std)
#     df2 = pd.read_sql(query2, conn_std)
#
#     if len(df1) == 0:
#         return {
#             "msg": "企业法人最新婚姻状态",
#             "idcard": idcard,
#             "status": "未婚"
#         }
#     elif (len(df1) > 0) and (len(df2) == 0):
#         df1_jhdjrq = df1.iloc[0]["jhdjrq"]
#         return {
#             "msg": "企业法人最新婚姻状态",
#             "idcard": idcard,
#             "status": "已婚",
#             "jhdjrq": df1_jhdjrq
#
#         }
#     elif (len(df1) > 0) and (len(df2) > 0):
#         df1_jhdjrq = df1.iloc[0]["jhdjrq"]
#         df2_lhdjrq = df2.iloc[0]["lhdjrq"]
#         if df1_jhdjrq > df2_lhdjrq:
#             return {
#                 "msg": "企业法人最新婚姻状态",
#                 "idcard": idcard,
#                 "status": "已婚",
#                 "jhdjrq": df1_jhdjrq
#             }
#         else:
#             return {
#                 "msg": "企业法人最新婚姻状态",
#                 "idcard": idcard,
#                 "status": "离异",
#                 "lhdjrq": df2_lhdjrq
#             }
#
#
# @router.post(path="/legal_person_latest_divorce_date", summary="法人最近离婚日期")
# def calc_legal_person_latest_divorce_date(data=Body(None)):
#     idcard = data["idcard"]
#     query = f"""
# 			select
# 				lhdjrq
# 			from std.std_smzj_hy_lhdjtz
# 			where sfzjh_n = '{idcard}' or sfzjh_nv = '{idcard}'
# 			order by lhdjrq desc limit 1"""
#     df = pd.read_sql(query, conn_std)
#     if len(df) > 0:
#         lhdjrq = df.iloc[0]["lhdjrq"]
#         return {
#             "msg": "法人最近离婚日期",
#             "idcard": idcard,
#             "lhdjrq": lhdjrq
#         }
#     else:
#         return {
#             "msg": "法人最近离婚日期",
#             "idcard": idcard,
#             "info": "此人处于结婚状态"
#         }
#
# @router.post(path="/legal_person_latest_divorce_count", summary="企业法人离婚总次数")
# def calc_legal_person_latest_divorce_count(data=Body(None)):
#     idcard = data["idcard"]
#     query1 = f"""
# 				select
# 					count(*) as cnt
# 				from std.std_smzj_hy_lhdjtz
# 				where sfzjh_n = '{idcard}' or sfzjh_nv = '{idcard}' """
#     cnt = int( pd.read_sql(query1, conn_std).iloc[0]["cnt"])
#     return {
#         "msg": "企业法人离婚总次数",
#         "idcard": idcard,
#         "count": cnt
#     }
